#include "../http_server.hpp"
#define WWWROOT "../wwwroot/"

void Login(const Http::HttpRequest & req,Http::HttpResponse * rsp)
{
    std::string res = req._body;
    size_t pos = res.find('&');
    std::string username = res.substr(0,pos);
    std::string password = res.substr(pos+1);
    size_t posname = username.find('=');
    std::string name = username.substr(posname+1);
    size_t posword = password.find('=');
    std::string word = password.substr(posword+1);
    if(name == "admin"&&word == "123456")
    {
        rsp->SetContent("OK");
    }
    else
    {
        rsp->SetContent("账号或密码错误");
    }
}

int main()
{
    if (daemon(1, 0) < 0) {
        std::cerr << "Failed to set daemon mode" << std::endl;
        exit(1);
    }
    Http::HttpServer server(8080);
    server.SetThreadCount(3);//线程数
    server.SetBaseDir(WWWROOT);//相对根目录
    server.Post("/login",Login);//post路由
    server.Listen();
    return 0;
}